package personal.wang.cli;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import org.rocksdb.Options;
import org.rocksdb.RocksDB;
import org.rocksdb.RocksDBException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import personal.wang.domain.HeartbeatBean;
import personal.wang.util.SystemInfoUtil;

/**
 * @className: Test
 * @Description:
 * @Author: wangyifei
 * @Date: 2025/2/24 14:29
 */
public class RockDBTest {
    private static Logger logger = LoggerFactory.getLogger(RockDBTest.class);

    public static void main(String[] args) {

        RocksDB.loadLibrary();
        Options options = new Options().setCreateIfMissing(true);
        String path = args[0];
        RocksDB db = null;
        try {
            db = RocksDB.open(options, path);
        } catch (RocksDBException e) {
            e.printStackTrace();
        }
        HeartbeatBean.Endpoint endpoint = HeartbeatBean.Endpoint.newBuilder()
                .setHost(SystemInfoUtil.getLocalHost())
                .setIp(SystemInfoUtil.getLocalIP())
                .build();
        HeartbeatBean.Report report = HeartbeatBean.Report.newBuilder()
                .setModel("")
                .setMessageCnt(0D)
                .build();
        HeartbeatBean.Heartbeat heartbeat = HeartbeatBean.Heartbeat.
                newBuilder()
                .setEndpoint(endpoint)
                .setReport(report)
                .build();
        ByteString hostBytes = heartbeat.getEndpoint().getHostBytes();
        try {
            db.put(hostBytes.toByteArray(), heartbeat.toByteArray());
            HeartbeatBean.Heartbeat heartbeat1 = HeartbeatBean.Heartbeat.parseFrom(db.get(hostBytes.toByteArray()));
            System.out.println(heartbeat1.getEndpoint().getHostBytes());
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        } catch (RocksDBException e) {
            e.printStackTrace();
        }
    }
}
